<?php
    include_once("../../includes/config.inc.php");
    check_login();
    $suserSession=get_suser();
    $customer_id=$suserSession['id'];
    $model=D('message');
    $Member=D('member');

    if($_REQUEST['a']=='sendMsg'){
        $id=(int)$_POST['id'];
        $row=$model->find($id);
        $reply_list=$row['reply_list'].$_POST['content']."&nbsp;|&nbsp;".date('y-m-d H:i').'<br />';
        $res=$model->updateById($id,array('is_reply'=>'1','reply_list'=>$reply_list));
        //发送客服消息
        if($res){
            $jsonTouser=$row['fromuser'];
            $jsonContent=deletehtml($_POST['content']);
            
            $jsonMsgtype="text";
            $configModel=new Model_CustomerConfig();
            $access_token=$configModel->getToken($row['customer_id']);
            $postUrl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
            $jsonData="{
                \"touser\":\"$jsonTouser\",
                \"msgtype\":\"$jsonMsgtype\",
                \"text\":
                {
                     \"content\":\"$jsonContent\"
                }
            }";
            curl_request($postUrl, $jsonData);
            die('suc');
        }
        die('err');
    }

    $timeLine=time()-48*3600;//48小时分界线
    $filter['where'] = "msgtype != 'event' and msg_content != ''";

    //区域账号
    if($suserSession['type']==2){
        $customerModel=D('customer');
        //区域下的门店
        $customerList=$customerModel->where("pid='".$customer_id."' and dele_state='1'")->dataArr();
        $smarty->assign('customerList',$customerList);
        
        $shopArr=array();
        if($customerList){
            foreach($customerList as $k=>$v){
                $shopArr[]=$v['id'];
            }
            $filter['where'].=" and customer_id in (".implode(',',$shopArr).") ";
        }else{
            $filter['where'].=" and customer_id = -1 ";
        }
    //门店账号
    }elseif($suserSession['type']==3){
        $filter['where'].=" and customer_id='{$customer_id}'";
    }
    
    if($_REQUEST['act']=='history'){
        $filter['where'].=" and create_time < '{$timeLine}'";
    }else{
        $filter['where'].=" and create_time > '{$timeLine}'";
    }
    if($_REQUEST['keyword']) $filter['where'].=" and msg_content like '%".$_REQUEST['keyword']."%'";

    $filter['order'] = "id desc";
    $data = $model->paginate($filter,'*',common_pg('p'),10);
    $listArr = $data['data'];

    foreach($listArr as $k=>$v){
        $uRow=$Member->where("fromuser='".$v['fromuser']."'")->dataRow();
        $listArr[$k]['headimgurl']=$uRow['headimgurl'];
        $listArr[$k]['nickname']=$uRow['nickname'];
    }
    $smarty->assign('list',$listArr);
    $smarty->assign('page',$model->pager($data['pager']));

    //敏感词
    $row=array();
    $arr = D('wx_sense')->dataArr();
    foreach($arr as $k=>$v){
        if($v['word']) $row[]=$v['word'];
    }
    $senseRow = $row ? json_encode($row) : '';
    $smarty->assign('senseRow',$senseRow);

    $smarty->setTpl('member/templates/interact.html')->display();
